<?php

declare(strict_types=1);
/*
 * @Description: Telegram service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-01-16 17:04:47
 */

namespace App\Service\Push;

use App\Common\RedisCache;
use Yurun\Util\HttpRequest;

class Telegram
{
    protected $serverInfo = [];
    protected $http = null;
    protected $redisCache = null;
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
        $this->http = new HttpRequest;
        $this->redisCache = new RedisCache();
    }

    /**
     * 群机器人
     */
    public function pushBotText(array $params)
    {
        $apiToken = 'YOUR_API_TOKEN';
        $chatId = 'CHAT_ID';
        $message = 'Hello, world!';

        $url = 'https://api.telegram.org/bot' . $apiToken . '/sendMessage';
        $data = array('chat_id' => $chatId, 'text' => $message);

        $options = array(
                'http' => array(
                    'method'  => 'POST',
                    'content' => json_encode($data, JSON_UNESCAPED_UNICODE),
                    'header' =>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n"
                )
            );

        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        $response = json_decode($result);

        print_r($response);

        $postFsQun['msg_type'] = 'text';
        $postFsQun['content']['text'] = $params['msg_content'];
        $timeStamp = time();
        $sign = base64_encode(hash_hmac('sha256', '', $timeStamp . "\n" . $this->serverInfo['platform_alert_feishuqun_secret'], true));
        $postFsQun['timestamp'] = $timeStamp;
        $postFsQun['sign'] = $sign;
        $updateLog['postFsQun'] = $postFsQun;
        $response = $this->http->post($this->serverInfo['platform_alert_feishuqun_webhook_url'], $postFsQun, 'json');
        return json_decode($response->body(), true);
    }
    public function pushBotTuwen(array $params)
    {
        $postFsQun['msg_type'] = 'post';
        $msgContent = json_decode($params['msg_content'], true);
        $postContent['zh_cn']['title'] = $params['title'];
        $postContentItem = [];
        $postContentItem[] = ['tag' => 'text', 'text' => $params['content']];
        if (isset($msgContent['url']) && !empty($msgContent['url'])) {
            $postContentItem[] = ['tag' => 'a', 'text' => '请查看', 'href' => $msgContent['url']];
        }
        $postContent['zh_cn']['content'] = $postContentItem;
        $postFsQun['content']['post'] = [$postContent];
        $timeStamp = time();
        $sign = base64_encode(hash_hmac('sha256', '', $timeStamp . "\n" . $this->serverInfo['platform_alert_feishuqun_secret'], true));
        $postFsQun['timestamp'] = $timeStamp;
        $postFsQun['sign'] = $sign;
        $response = $this->http->post($this->serverInfo['platform_alert_feishuqun_webhook_url'], $postFsQun, 'json');
        return json_decode($response->body(), true);
    }
    public function pushBotMD(array $params)
    {
        $postFsQun['msg_type'] = 'interactive';
        $postFsQun['card']['elements'][0]['tag'] = 'div';
        $postFsQun['card']['elements'][0]['text']['content'] = $params['msg_content'];
        $postFsQun['card']['elements'][0]['text']['tag'] = 'lark_md';
        $postFsQun['card']['header']['title']['content'] = $params['title'];
        $postFsQun['card']['header']['title']['tag'] = 'plain_text';
        $timeStamp = time();
        $sign = base64_encode(hash_hmac('sha256', '', $timeStamp . "\n" . $this->serverInfo['platform_alert_feishuqun_secret'], true));
        $postFsQun['timestamp'] = $timeStamp;
        $postFsQun['sign'] = $sign;
        $response = $this->http->post($this->serverInfo['platform_alert_feishuqun_webhook_url'], $postFsQun, 'json');
        return json_decode($response->body(), true);
    }
}
